//
//  ZFBChatController.m
//  HMPay
//
//  Created by HaoYoson on 16/7/1.
//  Copyright © 2016年 HaoYoson. All rights reserved.
//

#import "Masonry.h"
#import "NSArray+Addition.h"
#import "YHKeyboardListener.h"
#import "ZFBChat.h"
#import "ZFBChatController.h"
#import "ZFBChatMeCell.h"
#import "ZFBChatOtherCell.h"

static NSString *meCellid = @"me_cell";
static NSString *otherCellid = @"other_cell";

@interface ZFBChatController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSArray *chatData;

@end

@implementation ZFBChatController

//- (instancetype)init {
//    return [super initWithStyle:UITableViewStyleGrouped];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setupUI];

    self.chatData = [self loadChatData];
}

- (void)setupUI {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];

    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];

    self.tableView = tableView;

    // 设置代理对象
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    // 注册单元格
    [self.tableView registerClass:[ZFBChatMeCell class] forCellReuseIdentifier:meCellid];
    [self.tableView registerClass:[ZFBChatOtherCell class] forCellReuseIdentifier:otherCellid];
    // 取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // 行高
    self.tableView.estimatedRowHeight = 60;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    // 底边多44
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 44, 0);

    // 取消滚动条
    self.tableView.showsVerticalScrollIndicator = NO;

    // 模糊视图
    UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    UIVisualEffectView *barView = [[UIVisualEffectView alloc] initWithEffect:effect];
    [self.view addSubview:barView];

    // 文本框
    UITextField *text = [[UITextField alloc] init];
    text.borderStyle = UITextBorderStyleRoundedRect;
    [barView.contentView addSubview:text];

    // 发送按钮
    UIButton *send = [UIButton buttonWithType:UIButtonTypeSystem];
    [send setTitle:@"发送" forState:UIControlStateNormal];
    [barView.contentView addSubview:send];

    [text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(4);
        make.left.offset(8);
        make.bottom.offset(-4);
    }];

    [send mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(text.mas_right).offset(8);
        make.centerY.equalTo(text);
        make.right.offset(-8);
    }];

    [barView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.height.offset(44);
    }];

    // 监听键盘弹出 对barView做动画
    YHKeyboardListener *listener = [YHKeyboardListener sharedListener];

    __weak ZFBChatController *weakSelf = self;
    listener.willShow = ^(CGRect rect) {
        [barView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.offset(-rect.size.height);
        }];

        [UIView animateWithDuration:.2 animations:^{
            [weakSelf.view layoutIfNeeded];
        }];
    };

    listener.willHide = ^() {
        [barView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.offset(0);
        }];

        [UIView animateWithDuration:.2 animations:^{
            [weakSelf.view layoutIfNeeded];
        }];
    };

    // 拖拽的时候关闭键盘
    tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.chatData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 获取数据
    ZFBChat *chat = self.chatData[indexPath.row];

    if (chat.type) {    // 其他
        ZFBChatOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:otherCellid forIndexPath:indexPath];
        cell.chat = chat;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {    // 我
        ZFBChatMeCell *cell = [tableView dequeueReusableCellWithIdentifier:meCellid forIndexPath:indexPath];
        cell.chat = chat;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 4;
}

- (NSArray *)loadChatData {
    NSArray *data = [NSArray objectListWithPlistName:@"messages.plist" clsName:@"ZFBChat"];

    NSMutableArray *array = [NSMutableArray arrayWithArray:data];

    NSString *tempTime = [array[0] time];

    for (int i = 0; i < array.count; i++) {
        if (!i) {    // 第一个肯定一样 不需要去重
            continue;
        }
        if ([[array[i] time] isEqualToString:tempTime]) {
            [array[i] setTime:@""];
        } else {
            tempTime = [array[i] time];
        }
    }
    return array.copy;
}

- (void)dealloc {
    [[YHKeyboardListener sharedListener] clear];
}

@end
